/**
 * 试题异步
 */

function question_score_ajax(){
    var _this = this;
    _this.body_id;

    _this.initMethod = function(body_id, exam_no){
        _this.body_id = body_id;
        _this.intTable(exam_no);
    };

    _this.intTable = function (exam_no) {
        var url = appPath + "/popup/question/exam_score_ajax";
        var param = {"exam_no" : exam_no};
        var data = _this.ajax(url, param);
        var code = data['code'];
        if(code == '10000'){
            var questions = data['result'];
            var html = '';
            if(questions.length < 1){
                html = '<div><div class="no-content"></div></div>';
            }

            for(var i=0; i<questions.length; i++){
               var question = questions[i];
                var chrType = question.chrType;
                if(chrType == '01' || chrType == '02'){
                    html += _this.single_many_html(question, i);
                }else if(chrType == '03'){
                    html += _this.judge_html(question, i);

                    // 填空题
                }else if(chrType == '04'){
                    html += _this.fill_html(question, question_div_num + i);
                    card_count += 1;

                    // 简答 论述 计算  绘图
                } else if(chrType == '05' || chrType == '06' || chrType == '07' || chrType == '08') {
                    html += _this.subjective_html(question, question_div_num + i);
                    card_count += 1;
                }
            }

            $("#" + _this.body_id).append(html);
        }
    };

    _this.single_many_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions"  data-question="' + question.intId + '"  data-index="' + i + '">';
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（';
        if(chrType == '02'){
            html += '多选题';
        }else{
            html += '单选题';
        }
        html += '）</span><div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img class="lazy" data-original="' + file.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        var options = question.options;
        if(!options){
           return;
        }
        for(var i=0, length=options.length; i<length; i++){
            var q = options[i];
            html += '<div>';
            if(chrType == '02'){
                html += '<div class="option checkbox" data-type="' + q.oType + '">';
            }else{
                html += '<div class="option" data-type="' + q.oType + '">';
            }
            html += '<div><span>' + q.oType + '、</span><div>' + q.optionContent + '</div></div>';
            html += '</div>';
            html += '<div class="show_img clearfix">';
            if(q.mimeType == 'img'){
                html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
            }
            html += '</div>';
            html += '</div>';
        }
        html += '</div>';

        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }
        html += '我的回答：' + myAnswer + '（';
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<span class="is_correct" style="color: #00b8c7">回答正确</span>';
        }else{
            html += '<span class="is_correct" style="color: red;">回答错误</span>';
        }
        html += '）</div>';
        html += '<div class="pull-right">';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<a href="javascript:;" class="analy">展开解析 <i class="fa fa-angle-down"></i></a>';
        }else{
            html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-up"></i></a>';
        }
        html += '</div>';
        html += '</div>';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<div class="subject_foot">';
        }else{
            html += '<div class="subject_foot" style="display: block;">';
        }
        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';
        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles == undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(q.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };

    _this.judge_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions"  data-question="' + question.intId + '" data-index="' + i + '">';
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（判断题）</span><div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
                }
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        html += '<div>';
        html += '<div class="option" data-type="A">';
        html += '<div><span>A、</span><div>正确</div></div>';
        html += '</div>';
        html += '</div>';
        html += '<div>';
        html += '<div class="option" data-type="B">';
        html += '<div><span>B、</span><div>错误</div></div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';

        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }
        html += '我的回答：' + myAnswer + '（';
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<span class="is_correct" style="color: #00b8c7">回答正确</span>';
        }else{
            html += '<span class="is_correct" style="color: red;">回答错误</span>';
        }
        html += '）</div>';
        html += '<div class="pull-right">';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<a href="javascript:;" class="analy">展开解析 <i class="fa fa-angle-down"></i></a>';
        }else{
            html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-down"></i></a>';
        }
        html += '</div>';
        html += '</div>';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<div class="subject_foot">';
        }else{
            html += '<div class="subject_foot" style="display: block;">';
        }
        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';
        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles == undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(q.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };


    // 其他主观题
    _this.subjective_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '">';

        html += '<div class="subject"><div class="question_content clearfix">';

        // 题型名称
        var titleTypeName = "";
        if (chrType == '05') {
            titleTypeName = "（简答题）";
        } else if (chrType == '06') {
            titleTypeName = "（论述题）";
        } else if (chrType == '07') {
            titleTypeName = "（计算题）";
        } else if (chrType == '08') {
            titleTypeName = "（绘图题）";
        }

        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">' + titleTypeName + '</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';


        html += '<div class="options">';

        html += '<div>';
        html += '<div>';
        html += '<span></span>';
        html += '<div><span>答案、</span><div><textarea style="width: auto" class="answer-input"></textarea></div></div>';
        html += '</div>';
        html += '</div>';

        html += '</div>';



        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info subjective">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        html += '<a href="javascript:;" class="analy" style="display: none;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="subject_foot">';

        // 3 学员练习主观题权限-【展示答案】
        if (_this.practiceLimits == 3) {
            html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';

            // ***************************begin
            html += '<div class="show_img clearfix">';
            var varAnswerImages = question.varAnswerImages;
            if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
                var answerImagesArr = varAnswerImages.split(",");
                for (var j = 0; j <answerImagesArr.length ; j++) {
                    html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
                }
            }
            html += '</div>';

            // ***************************end

            //4 学员练习主观题权限-【不展示答案】
        } else  if (_this.practiceLimits == 4) {
            html += '<div><span class="label">正确答案：</span><i class="answer">【对不起，管理员尚未公开该课程的主观题参考答案，您暂时无法查看】。</i></div>';
        }

        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;

            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl +'">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;

    };

    // 填空题
    _this.fill_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '">';

        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（填空题）</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';


        html += '<div class="options">';

        var titNum = checkTianKongNum(question.varQuestionsContent);
        for (var j = 0; j < titNum; j++) {

            html += '<div>';
            html += '<div>';
            html += '<span></span>';
            html += '<div><span>空 '+ (j+1) +' 答案、</span><div><input type="text" width="60px" class="answer-input"/></div></div>';
            html += '</div>';
            html += '</div>';

        }

        html += '</div>';


        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        html += '<button type="button" class="btn btn-info subjective">确定</button>&nbsp;&nbsp;';
        html += '<button type="button" class="btn btn-mark">标记</button>';
        html += '</div>';
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="remove_error" style="display: none;"><i class="fa fa-remove"></i> 移除错题</a>&nbsp;&nbsp;';
        html += '<a href="javascript:feed_back(\''+question.intId+'\');" class="wrong"><i class="fa fa-times"></i> <span>错题反馈</span></a>&nbsp;&nbsp;';
        var chrIsCollect = question.chrIsCollect;
        if(chrIsCollect == true || chrIsCollect == 'true'){
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star on"></i> <span>取消收藏</span></a>&nbsp;&nbsp;';
        }else{
            html += '<a href="javascript:;" class="collect"><i class="fa fa-star-o" ></i> <span>收藏试题</span></a>&nbsp;&nbsp;';
        }
        html += '<a href="javascript:;" class="analy" style="display: none;">展开解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="subject_foot">';

        // 3 学员练习主观题权限-【展示答案】
        if (_this.practiceLimits == 3) {
            html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';

            // ***************************begin
            html += '<div class="show_img clearfix">';
            var varAnswerImages = question.varAnswerImages;
            if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
                var answerImagesArr = varAnswerImages.split(",");
                for (var j = 0; j <answerImagesArr.length ; j++) {
                    html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
                }
            }
            html += '</div>';

            // ***************************end

            //4 学员练习主观题权限-【不展示答案】
        } else  if (_this.practiceLimits == 4) {
            html += '<div><span class="label">正确答案：</span><i class="answer">【对不起，管理员尚未公开该课程的主观题参考答案，您暂时无法查看】。</i></div>';
        }


        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };

    // 检查题目内容括号个数
    function checkTianKongNum(title) {

        // 正则表达式
        var reg = /[\(|（]([^\(\)（）]*)[\)|）]/ig;

        var t = title.match(reg);

        if (t == null) {
            return 0;
        }

        // 总共括号个数
        var num = title.match(reg).length;

        // 返回括号个数
        return num;

    }

    _this.ajax = function(url, param, type) {
        var result;
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : param,
            success : function(data) {
                result = data;
            }
        });
        return result;
    };
}

var question_score_ajax = new question_score_ajax();